/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.dao.impl;

import com.ivanbiz.dao.HistoryPenghuniDAO;
import com.ivanbiz.model.Fasilitas;
import com.ivanbiz.model.HistoryPenghuni;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.model.Penghuni;
import com.ivanbiz.service.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author City_Z
 */
public class HistoryPenghuniDAOImpl extends GenericDAOImpl implements HistoryPenghuniDAO {

    @Override
    public String tutupPenghuni(Penghuni penghuni) throws Exception {
        String status = "";
        Query query = null;
        try {
            HibernateUtil.beginTransaction();
            Session session = HibernateUtil.getSession();
            query = (Query) session.createQuery("from com.ivanbiz.model.Kamar k where k.penghuni.id ='"+penghuni.getId()+"'");
            Kamar kamar = (Kamar)query.uniqueResult();
            kamar.setPenghuni(null);
            kamar.setTarif(0);
            session.update(kamar);
            query = (Query)session.createQuery("from com.ivanbiz.model.Fasilitas f where f.kamar.id ='"+kamar.getId()+"'");
            Fasilitas fasilitas = (Fasilitas)query.uniqueResult();     
            fasilitas.setItemFasilitas(null);
            session.update(fasilitas);
            HistoryPenghuni historyPenghuni = new HistoryPenghuni();
            historyPenghuni.setNama(penghuni.getNama());
            historyPenghuni.setAlamat(penghuni.getAlamat());
            historyPenghuni.setTelp(penghuni.getTelp());
            historyPenghuni.setIdIdentitas(penghuni.getIdIdentitas());
            session.save(historyPenghuni);
            session.delete(penghuni);
            HibernateUtil.commitTransaction();
            status = "sukses";
        } catch (Exception e) {
            HibernateUtil.rollbackTransaction();
            status = "gagal";
            throw e;
        } finally {
            HibernateUtil.closeSession();
        }
        return status;
    }
}
